package com.thjk.bdf.preloan.web.service.impl;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thjk.bdf.common.base.BasicResp;
import com.thjk.bdf.common.enmus.ProductEnum;
import com.thjk.bdf.common.enmus.ServiceEnum;
import com.thjk.bdf.preloan.web.service.MisService;

@Service
public class MisServiceImpl implements MisService{
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private RestTemplate restTemplate;
	
	@Value("${mas.sendV2.url}")
	private String mas2SendUrl; //mis二代服务

	@Override
	public BasicResp phoneCheck(String phone) {
		
		Map<String, Object> resultMap = new HashMap<String, Object>(); //返回结果
		resultMap.put("code", "1");
		BasicResp resp = new BasicResp();
		try {
			Map<String,Object> reqJson = new HashMap<String,Object>();
			MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
			reqJson.put("phone", phone);
			reqJson.put("status", "1"); //1-正常
			
			params.add("topicName", "operation.queryEmp");
			params.add("messgeJsonBody", JSONObject.toJSONString(reqJson));
			
			logger.info("CreditApplyController/ajaxVerifyMgrPhone param is :"+ params);
			
			JSONObject retStr = restTemplate.postForObject(mas2SendUrl, params, JSONObject.class);
			logger.info("CreditApplyController/ajaxVerifyMgrPhone retStr is :"+ retStr);
			
			if(CollectionUtils.isEmpty(retStr)||retStr.getInteger("totalCount")==0) {
	        	resp.setCode("0");
	        	resp.setMessage("未查询到此客户经理手机号，请联系您的客户经理确认！");
	        	return resp;
			}
			//验证手机号码对应的客户经理是否有销售权限
			JSONArray recordList = retStr.getJSONArray("recordList");
			JSONObject record  = (JSONObject) recordList.get(0);
			String productAuth = record.get("productAuth")==null?"":record.get("productAuth").toString();
			String[] products = productAuth.split(",");
			for(String product: products){
				if(ProductEnum.P_300002.getProductCode().equals(product)){
					return BasicResp.success();
				}
			}
			resp.setCode(ServiceEnum.PHONE_CHECK_NOAUTH.getCode());
			resp.setMessage(ServiceEnum.PHONE_CHECK_NOAUTH.getDesc());
			return resp;
		} catch (Exception e) {
			StringWriter w = new StringWriter();
            e.printStackTrace(new PrintWriter(w));
            logger.error(w.toString());
            
            resultMap.put("code", "0");
            resultMap.put("message", "系统异常，请稍后再试");
            return BasicResp.error();
		}
	}

}
